package org.sadko.boozemeter;

import java.io.File;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ToggleButton;
import android.widget.CompoundButton.OnCheckedChangeListener;


public class BoozeMeter extends Activity {

public static final int MESSAGE_EXIT = 2;	
public static MyHandler mHndl;
public boolean mSoundOn = false;
public Menu mMenu;
public Dialog calibrDlg;

	private class MyHandler extends Handler{
		@Override
		public void handleMessage(Message msg){
			switch(msg.what){
				case MESSAGE_EXIT:{
				finish();
			}
			}
		}
	}
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mHndl = new MyHandler();
        
        Button button = (Button) this.findViewById(R.id.start_button);
        button.setOnClickListener(new OnClickListener() {
        	public void onClick(View v){
        		Intent intent = new Intent(BoozeMeter.this, TremorSensor.class);
        		intent.putExtra("sound_state", mSoundOn);
        		BoozeMeter.this.startActivity(intent);

        	}
        });
        
        /*button = (Button) this.findViewById(R.id.about_button);
        button.setOnClickListener(new OnClickListener() {
        	public void onClick(View v){
        		Intent intent = new Intent(BoozeMeter.this, AboutActivity.class);
        		BoozeMeter.this.startActivity(intent);

        	}
        });*/
        
        //Prepare the dialog for calibration how-to
        calibrDlg = new AlertDialog.Builder(BoozeMeter.this
			).setIcon(org.sadko.boozemeter.R.drawable.icon_normal
			).setTitle("How to"
			).setMessage(org.sadko.boozemeter.R.string.calibration_howto_dialog
			).setPositiveButton("Ok", new DialogInterface.OnClickListener(){
				public void onClick(DialogInterface dialog, int which) {
					Intent intent = new Intent(BoozeMeter.this, Calibrator.class);
					startActivity(intent);
				}	
			}
			).setNegativeButton("Cancel", null
			).create();
		
        /*button = (Button) this.findViewById(R.id.calibrate_button);
        button.setOnClickListener(new OnClickListener() {
        	public void onClick(View v){
        		calibrDlg.show();
        	}
        });*/
        
        ToggleButton tb = (ToggleButton) findViewById(R.id.sound_button);
        tb.setOnCheckedChangeListener(new OnCheckedChangeListener(){

			public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
				mSoundOn = arg1;

			}
        	
        });
        
        File root = new File("/data/data/org.sadko.boozemeter/");
        File calibration = new File(root, "calibration.bzm");
        
        if (calibration.length() == 0){
           	Dialog dlg = new AlertDialog.Builder(BoozeMeter.this
	        	).setIcon(org.sadko.boozemeter.R.drawable.icon
				).setTitle(":("
				).setMessage(org.sadko.boozemeter.R.string.not_calibrated_dialog
				).setPositiveButton("Ok", new DialogInterface.OnClickListener(){
					public void onClick(DialogInterface dialog,int which) {
						calibrDlg.show();
					}
				}
				).setNegativeButton("Cancel", null
        		).create();
        	dlg.show();
        	
        }
        
    }
    
    @Override
    	public boolean onCreateOptionsMenu(Menu menu) {
    		mMenu = menu;
    		MenuInflater mInf = getMenuInflater();
    		mInf.inflate(R.menu.menu_main, menu);
    		return super.onCreateOptionsMenu(menu);
    	}
    
    @Override
    	public boolean onOptionsItemSelected(MenuItem item) {
    		switch(item.getItemId()){
    		case R.id.item_about: {
    			Intent intent = new Intent(BoozeMeter.this, AboutActivity.class);
        		BoozeMeter.this.startActivity(intent);
    			break;
    		}
    		case R.id.item_calibration:{
    			calibrDlg.show();
    			break;
    		}
    		}
    		return super.onOptionsItemSelected(item);
    	}


}